﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;

namespace SchoolManagementSystem.Details
{
    //lop hoc
    public partial class ClassUC : DetailUC
    {
        public ClassUC()
        {
            InitializeComponent();
            this.ObjectName = "lớp học";
            this.Code = "Class";
            this.Type = new Class();

            comboboxUC_Course.Model = RegisterService.RegisterServices.CourseService;
            comboboxUC_Course.Uc = new CourseUC();
            comboboxUC_Course.ColumnMain = "Name";
            comboboxUC_Course.ColumnDisplay = "Code,Name";

            comboboxUC_Teacher.SearchString =" Teacher.Active =1";
            comboboxUC_Teacher.Model = RegisterService.RegisterServices.TeacherService;
            comboboxUC_Teacher.Uc = new TeacherUC();
            comboboxUC_Teacher.ColumnMain = "Name";
            comboboxUC_Teacher.ColumnDisplay = "Code,Name";

            comboboxUC_Branch.SearchString = " ParentID <>0";
            comboboxUC_Branch.Model = RegisterService.RegisterServices.BranchService;
            comboboxUC_Branch.Uc = new BranchUC();
            comboboxUC_Branch.ColumnMain = "Name";
            comboboxUC_Branch.ColumnDisplay = "Code,Name";
            comboboxUC_Branch.combobox.EditValueChanged += new EventHandler(comboboxUC_BranchlookUp_EditValueChanged);


           // spinEdit_siso.Enabled = false;

            masterMainUC1.Model = RegisterService.RegisterServices.Class_FeeService;
            masterMainUC1.DetailUCControl = new Class_FeeTimeUC();

            spinEdit_siso.EditValue = 0;
        }


        void comboboxUC_BranchlookUp_EditValueChanged(object sender, EventArgs e)
        {
           if(ID<=0)
           {
               if (comboboxUC_Branch.ID > 0)
               {
                   var codebranch = ((Branch)RegisterService.RegisterServices.BranchService.GetByID(comboboxUC_Branch.ID)).Code;
                   txtcode.Text = txtname.Text = RegisterService.RegisterServices.ClassService.GenCodeClass(codebranch);
               }
           }
        }

        String ClassID = "1=1";
        protected override long DetailDisplay()
        {
            Class data = new Class();
            if (ID > 0)
            {
                data = (Class)RegisterService.RegisterServices.ClassService.GetByID(ID);
                ClassID = data.ID.ToString();
            }

            txtname.Text = data.Name;
            txtnote.Text = data.Description;
            txtcode.Text = data.Code;
            comboboxUC_Course.ID = data.CourseID;
            comboboxUC_Teacher.ID = data.MainTeacherID;
            dateEdit_StartDate.DateTime = data.StartDate;
            dateEdit_EndDate.DateTime = data.EndDate;
            comboboxUC_Branch.ID = data.BranchID;
            spinEdit_siso.EditValue = data.Size;
            txttime.Text = data.Study_Time;
            ckactive.Checked = data.Active;
            spinEdit_price.EditValue = data.Price;
            radioGroup_type.SelectedIndex = data.Type;

            masterMainUC1.SearchString = " ClassID='" + ClassID + "'";
            masterMainUC1.DetailUCControl.SearchString = " ClassID='" + ClassID + "'";
            return ID;

        }

        protected override long Save()
        {
            Class data = new Class();
            if (ID > 0)
            {
                data = (Class)RegisterService.RegisterServices.ClassService.GetByID(ID);

            }

            data.Code =txtcode.Text;// RegisterService.RegisterServices.ClassService.GenCodeClass(((Branch)RegisterService.RegisterServices.BranchService.GetByID(comboboxUC_Branch.ID)).Code);
            data.Name = data.Code;
            data.Active = ckactive.Checked;
            data.CourseID = comboboxUC_Course.ID;
            data.MainTeacherID = comboboxUC_Teacher.ID;
            data.Active = ckactive.Checked;
            data.Size = int.Parse(spinEdit_siso.EditValue.ToString());
            data.Study_Time = txttime.Text;
            data.BranchID = comboboxUC_Branch.ID;
            data.Type = radioGroup_type.SelectedIndex;
            data.StartDate = dateEdit_StartDate.DateTime;
            data.EndDate = dateEdit_EndDate.DateTime;
            data.Price = int.Parse(spinEdit_price.EditValue.ToString());
            
            ID = RegisterService.RegisterServices.ClassService.Save(data, ref ErrorMess);

            masterMainUC1.DetailUCControl.SearchString = ID.ToString();

            return ID;
        }


        public override bool Delete(long SelectedID)
        {
            var check = RegisterService.RegisterServices.StudentService.GetByClassID(SelectedID);
            if (check != null && check.Count != 0)
            {
                ErrorMess = "Vui lòng xóa các học viên liên quan trước khi xóa lớp học này";
                return false;
            }
            else
            {


                var classFeetimes = RegisterService.RegisterServices.Class_FeeService.GetListByLINQ(c => c.ClassID == SelectedID);
                if (classFeetimes != null)
                {
                    foreach (var item in classFeetimes)
                    {
                        RegisterService.RegisterServices.Class_FeeService.Delete(RegisterService.RegisterServices.Class_FeeService.GetByID(item.ID));
                    }
                }
                return RegisterService.RegisterServices.ClassService.Delete(RegisterService.RegisterServices.ClassService.GetByID(SelectedID));
              //  return RegisterService.RegisterServices.ClassService.UpdateIsDelete(RegisterService.RegisterServices.ClassService.GetByID(SelectedID));
            }
         //   return RegisterService.RegisterServices.ClassService.Delete(RegisterService.RegisterServices.ClassService.GetByID(SelectedID));

        }

        private void ClassUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(xtraTabPage1.Controls);
            AddTextChanged(xtraTabPage2.Controls);
        }

        private void radioGroup_type_EditValueChanged(object sender, EventArgs e)
        {
            if (ID < 0)
            {
                if (radioGroup_type.SelectedIndex == 0)
                {
                    dateEdit_EndDate.EditValue = DateTime.Now.AddMonths(2);
                }
                else
                {
                    dateEdit_EndDate.EditValue = DateTime.Now.AddMonths(6);
                }
            }
        }

      


    }
}
